import numpy as np
from flask import Flask, request, jsonify

app = Flask(__name__)
# 读取文件
question_dict = np.load('data/file.npy', allow_pickle=True).item()

@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'


@app.route('/test', methods=['POST'])
def taxApi():
    if request.method == 'POST':
        # 获取 POST 请求体中的 JSON 数据
        data = request.json

        # 在这里处理你的数据
        request_data = data.get('test', '')

        res = []
        mymap = {"0": "A", "1": "B", "2": "C", "3": "D", "4": "E", "5": "F", "6": "G", 'no': '无答案'}

        for i in request_data:
            if i in question_dict:
                temp = question_dict[i].split(",")
            else:
                temp = ['no']
            tmp_res = []
            for label in temp:
                tmp_res.append(mymap[label])
            res.append(tmp_res)
        response_data = {}
        for i in res:
            response_data[f"答案:"] = i
        # 返回处理后的结果
        return jsonify({'message_processed': response_data}), 200

    else:
        # 如果不是 POST 请求，则返回错误信息
        return jsonify({'error': 'Only POST requests are allowed'}), 405


if __name__ == '__main__':
    app.run(port=6088)
